package com.breadenglish.cms.api.app.gateway.service.impl;

import com.breadenglish.cms.api.app.common.utils.PropertyConfigurerUtils;
import com.breadenglish.cms.api.app.dao.WordDao;
import com.breadenglish.cms.api.app.domain.constants.Constants;
import com.breadenglish.cms.api.app.domain.db.Word;
import com.breadenglish.cms.api.app.domain.query.WordQuery;
import com.breadenglish.cms.api.app.gateway.model.WordOfTheDay;
import com.breadenglish.cms.api.app.gateway.model.WordOfTheDaySubTitle;
import com.breadenglish.cms.api.app.gateway.service.WordOfDayService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by lixuegang on 2016/1/8.
 */
@Service("wordOfDayService")
public class WordOfDayServiceImpl implements WordOfDayService {
    @Resource
    private WordDao wordDao;

    @Override
    public List<WordOfTheDay> queryWordOfTheDay() throws Throwable {
        List<WordOfTheDay> result = new ArrayList<WordOfTheDay>();
        // 查询所有未删除并且已发布的数据
        List<WordQuery> words = wordDao.queryWord(new Word(Constants.RELEASE_STATE_FOR_WORD, Constants.NOT));
        if (CollectionUtils.isNotEmpty(words)) {
            for (WordQuery word : words) {
                WordOfTheDay wotd = WordOfTheDay.buildDetails(word.getWordId().intValue(), word.getWordTitle(), word.getSummary(), word.getUpdateTime(), word.getImagePath(), "fullSizeImage", null, true, PropertyConfigurerUtils.getProperty("domain.url") + "/likeWord/showDetailWord.html?wordId=" + word.getWordId());
                result.add(wotd);
            }
        }
        return result;
    }

    @Override
    public WordOfTheDay queryWordOfTheDayById(Long id) throws Throwable {
        WordQuery word = wordDao.selectWordById(id);
        return WordOfTheDay.buildDetails(word.getWordId().intValue(), word.getWordTitle(), word.getSummary(), word.getUpdateTime(), word.getImagePath(), "fullSizeImage", null, true, PropertyConfigurerUtils.getProperty("domain.url") + "/likeWord/showDetailWord.html?wordId=" + word.getWordId());
    }
}
